/**
 * @FILE:EmsPlvConfigManager.java
 * @AUTHOR:chenqiu
 * @DATE:2016年5月31日 下午2:04:14
 **/
package com.smarttest.common.beans.protolcol.plv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smarttest.common.util.protocol.ems.DspConfig;
import com.smarttest.common.util.serializer.JsonSerializer;



public class PlvConfigLoad{
	private String plvConfig="smart.plvconfig.reload.flag";
	private DspConfig dspConfig;
	private Logger log = LoggerFactory.getLogger(this.getClass());	
	private final String configPath="conf"+File.separator+"smart"+File.separator+"plv";
	public void start() {
		String value = dspConfig.getConfig("CORE", plvConfig);
		log.info("flag "+plvConfig+"\t"+value);
		/*dspConfig.setConfigListener("CORE", plvConfig, 
			new ManagerListener() {				
				@Override
				public void receiveConfigInfo(String configInfo) {
					log.info("[ SmartPLV 配置更新-"+configInfo+" ]");					
					String value = configInfo;
					startPlvLoad();			
					log.info("[ SmartPLV 配置更新完毕 ] value{}",value);
				}				
				@Override
				public Executor getExecutor() {
					return null;
				}
			});*/
		startPlvLoad();				
	}
	
	public void startPlvLoad(){
		try{
			File path=new File(configPath);
			File[] files=path.listFiles(
				new FileFilter(){
					@Override
					public boolean accept(File dir) {
						if(dir!=null&&StringUtils.isNotBlank(dir.toString())){
							return dir.toString().toLowerCase().endsWith(".plv");
						}
						return false;
					}
				});
			PlvConfigStorage.getPlvConfigs().clear();
			for (File file: files){
				log.info("filename: "+file.toString());
				PlvConfigStorage.addPlvConfigs(loadPlvConfig(file));	
			}	
		}catch(Exception ex){
			  log.error("读取文件路径报错",ex);
		}
	}
	
	
	public Map<Integer, PlvConfig> loadPlvConfig(File file)  {
    	try{
	        Map<Integer, PlvConfig> map = new ConcurrentHashMap<Integer, PlvConfig>();
	        FileInputStream fileinput=new FileInputStream(file);
	        InputStreamReader reader=new InputStreamReader(fileinput,"UTF-8");
	        BufferedReader br =new BufferedReader(reader);
	        try{
		        log.info("加载PLV配置开始。");	        
		        String line=null;
		        while ( (line=br.readLine())!=null) {
		        	line = line.trim();
		            if (line.startsWith("#")) {
		                continue;
		            }
		            if(StringUtils.isEmpty(line)){
		                continue;
		            }
		            try {
		            	PlvConfig plvconfig=JsonSerializer.toObject(line, PlvConfig.class);
		            	int id = Integer.parseInt(plvconfig.getId().substring(2), 16);
		            	map.put(id, plvconfig);
		            } catch (Exception e) {
						log.error("异常的PLV配置:"+line+e.getMessage(),e);
					}
		         }
		        log.info("加载PLV配置结束。");	        
	         }finally{
	        	try {				
					br.close();
					reader.close();
					fileinput.close();
				} catch (Exception e) {
					log.error("error in close io stream");
				}
	         }
             return map;
    	}catch(Exception ex){
    	   log.error("加载PLV配置文件报错",ex);
    	   return null;
    	}
    }
    public static void main(String[] args) { 
        new PlvConfigLoad().start();
        System.out.println(PlvConfigStorage.getPlvConfigs());
//        boolean rs= isSpecialOperator("s-100000.1");
//        System.out.println(rs);
    }
	public void setDspConfig(DspConfig dspConfig) {
		this.dspConfig = dspConfig;
	}	
}
